package solucionessgh.EstadoHabitacion;


import java.sql.Connection;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import javax.swing.JOptionPane;
import solucionessgh.Util.DataBase;

/**
 * @author roderick
 **/
public class EstadoHabitacionModel extends DataBase{
    
    private int id;//ehacod
    private String ehadesc;
    private String ehaabr;
    private String ehacol;
    private boolean ehaact;
    
    //ehacod,ehadesc,ehaabr,ehacol,ehaact

    public EstadoHabitacionModel(){

    }

    private boolean validar(){
        return true;
    }

    public boolean create(){
        if(validar()){
            try{
                Connection con = ConnectionBase();
                PreparedStatement stmt = con.prepareStatement("INSERT INTO estadohabitacion(ehadesc, ehaabr, ehacol, ehaact) VALUES (?, ?, ?, ?);");
                stmt.setString(1,this.ehadesc);
                stmt.setString(2,this.ehaabr);
                stmt.setString(3,this.ehacol);
                stmt.setBoolean(4,this.ehaact);
                stmt.executeUpdate();
                stmt.close();
                con.close();
                return true;
            }
            catch( Exception e ){
                JOptionPane.showMessageDialog(null,
                    e.getMessage(),
                    "Estado de Habitacion",
                    JOptionPane.ERROR_MESSAGE);
            }
        }
        return false;
    }

    public boolean update(){
        if(validar()){
            try{
                Connection con = ConnectionBase();
                PreparedStatement stmt = con.prepareStatement("UPDATE estadohabitacion SET ehadesc=?, ehaabr=?, ehacol=?, ehaact=? where ehacod = ? ;");
                stmt.setString(1,this.ehadesc);
                stmt.setString(2,this.ehaabr);
                stmt.setString(3,this.ehacol);
                stmt.setBoolean(4,this.ehaact);
                stmt.setInt(5,this.id);
                stmt.executeUpdate();
                stmt.close();
                con.close();
                return true;
            }
            catch( Exception e ){
                JOptionPane.showMessageDialog(null,
                    e.getMessage(),
                    "Estado de Habitacion",
                    JOptionPane.ERROR_MESSAGE);
            }
        }
        return false;
    }

    public boolean delete(){
        if(validar()){
            try{
                Connection con = ConnectionBase();
                PreparedStatement stmt = con.prepareStatement("delete from estadohabitacion where ehacod=?;");
                stmt.setInt(1, this.id);
                stmt.executeUpdate();
                stmt.close();
                con.close();
                return true;
            }
            catch( Exception e ){
                JOptionPane.showMessageDialog(null,
                    e.getMessage(),
                    "Estado de Habitacion",
                    JOptionPane.ERROR_MESSAGE);
            }
        }
        return false;
    }

    public ResultSet read(){
        return this.find("SELECT ehacod, ehadesc, ehaabr, ehacol, CASE WHEN ehaact=False THEN 'No' WHEN ehaact=True THEN 'Si' END as ehaact FROM estadohabitacion order by ehadesc;");
    }

    public ResultSet finder(String var){
        return this.find("SELECT ehacod, ehadesc, ehaabr, ehacol, CASE WHEN ehaact=False THEN 'No' WHEN ehaact=True THEN 'Si' END as ehaact from estadohabitacion  WHERE ehadesc LIKE '"+var+"%';");
    }

    public ResultSet finder(int var){
        return this.find("SELECT ehacod, ehadesc, ehaabr, ehacol,ehaact "+
                         "FROM estadohabitacion WHERE ehacod = "+var+";");
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getEhaabr() {
        return ehaabr;
    }

    public void setEhaabr(String ehaabr) {
        this.ehaabr = ehaabr;
    }

    public boolean isEhaact() {
        return ehaact;
    }

    public void setEhaact(boolean ehaact) {
        this.ehaact = ehaact;
    }

    public String getEhacol() {
        return ehacol;
    }

    public void setEhacol(String ehacol) {
        this.ehacol = ehacol;
    }

    public String getEhadesc() {
        return ehadesc;
    }

    public void setEhadesc(String ehadesc) {
        this.ehadesc = ehadesc;
    }
     
}
